package com.gai.web.video.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gai.web.system.entity.Message;
import com.gai.web.video.entity.GVideo;
import com.gai.web.video.service.GVideoService;
import com.gai.web.video.vo.VideoByTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Captain
 * @since 2022-5-11
 */
@RestController
@Api(value="影视",tags="影视")
@RequestMapping("/video")
public class GVideoController {

    @Autowired
    private  GVideoService gVideoService;
    @GetMapping("/list")
    @ApiOperation(value="分页查询",notes="分页查询")
    public Message queryPageList(@RequestParam(name = "pageNo", defaultValue = "1")Integer pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "3")Integer pageSize,
                                 @RequestParam(name = "searchName",required=false)String searchName,
                                 @RequestParam(name = "startDate",required=false)String startDate,
                                 @RequestParam(name = "endDate",required=false)String endDate,
                                 @RequestParam(name = "tid",defaultValue ="0")Integer tid,
                                 @RequestParam(name = "sid",defaultValue ="0")Integer sid
                                 )
    {
        QueryWrapper<GVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(searchName), "title", searchName);
        queryWrapper.ge(StringUtils.hasText(startDate), "date", startDate);
        queryWrapper.le(StringUtils.hasText(endDate), "date", endDate);
        queryWrapper.eq(tid!=0,"tid", tid);
        queryWrapper.eq(sid!=0,"sid", sid);
        queryWrapper.orderByDesc("date");
        Page<GVideo> page= new Page<GVideo>(pageNo, pageSize);
        IPage<GVideo> pageList=gVideoService.videoPage(page,queryWrapper);
        return Message.success("获取成功！").add("pageList", pageList);
    }

    @ApiOperation(value="获取总数",notes="获取总数")
    @GetMapping("/getCount")
    public Message queryCount(){
        QueryWrapper<GVideo> queryWrapper = new QueryWrapper<>();
        int video_num=gVideoService.count();
        return Message.success("获取成功! ").add("video_num", video_num);
    }
    /**
     * 新增影视
     */
    @ApiOperation(value="新增影视",notes="新增影视")
    @PostMapping("/add")
    public Message add(@RequestBody GVideo gVideo){
        if (gVideoService.save(gVideo)) {
            return Message.success("新增成功!");
        } else {
            return Message.error("新增失败!");
        }
    }
    /**
     * 编辑影视信息
     */
    @ApiOperation(value="编辑影视",notes="编辑影视")
    @PostMapping("/edit")
    public Message editVideo(@RequestBody GVideo gVideo){
        if(gVideoService.updateById(gVideo)){
            return Message.success("更新成功！");
        }else{
            return Message.error("更新失败！");
        }
    }
    /**
     * 删除影视信息
     */
    @ApiOperation(value="删除影视",notes="删除影视")
    @DeleteMapping("/delete")
    public Message deleteVideo(@RequestParam(name = "id")Integer id){
        if (gVideoService.removeById(id)) {
            return Message.success("删除成功！");
        }else {
            return Message.error("删除失败！");
        }
    }
    @ApiOperation(value="按类型统计影视信息",notes="按类型统计影视信息")
    @GetMapping("/getVideoByType")
    public Message getVideoByType(){
        List<VideoByTypeVo> videoByType = gVideoService.getVideoByType();
        if(videoByType.size()<1){
            return Message.error("获取失败！");
        }else{
            List labelList=videoByType.stream().map(VideoByTypeVo::getName).collect(Collectors.toList());
            return Message.success("获取成功！").add("labelList", labelList).add("data", videoByType);
//            return Message.success("获取成功！").add("data", songByYear);
        }
    }

}
